//
//  JPictureSelectorCell.swift
//  JMicroBlog
//
//  Created by Jason on 28/5/15.
//  Copyright (c) 2015 Shepherd. All rights reserved.
//

import UIKit
let JPictureSelectNotification = "JPictureSelectNotification"
let JPictureRemoveNotification = "JPictureRemoveNotification"

class JPictureSelectorCell: UICollectionViewCell {
    // 关闭按钮
    @IBOutlet weak var closeBtn: UIButton!
    // 显示图片按钮
    @IBOutlet weak var pictureBtn: UIButton!
    var image : UIImage? {
        didSet{
            // 当照片为空时隐藏关闭按钮 否则显示 contentMode是针对image而不是backgroundImage 切记
            closeBtn.hidden = image == nil
            if image != nil {
                pictureBtn.setImage(image, forState: UIControlState.Normal)
                pictureBtn.setBackgroundImage(nil, forState: UIControlState.Normal)
            } else {
                pictureBtn.setImage(nil, forState: UIControlState.Normal)
                pictureBtn.setBackgroundImage(UIImage(named: "compose_pic_add"), forState: UIControlState.Normal)
            }
        }
    }
    // 发布通知 交给撰写控制器去处理 视图只要提要求就好
    @IBAction func selectPicture() {
        NSNotificationCenter.defaultCenter().postNotificationName(JPictureSelectNotification, object: self)
    }
    
    @IBAction func removePicture() {
        NSNotificationCenter.defaultCenter().postNotificationName(JPictureRemoveNotification, object: self)
    }
    override func awakeFromNib() {
        pictureBtn.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
    }
}
